package sm.pzavoli.mercato;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class NewAdapter extends SimpleAdapter implements Filterable {	
	private ArrayList<HashMap<String, String>> mAllData;
	private ArrayList<HashMap<String, String>> mDataShown;

	public NewAdapter(Context context, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);	
		mAllData = (ArrayList<HashMap<String, String>>) data;
		mDataShown = (ArrayList<HashMap<String, String>>) mAllData.clone();
		//mAllData = (ArrayList<HashMap<String, String>>) mDataShown.clone();		
	}

	@Override
	 public Filter getFilter() {
	  Filter nameFilter = new Filter(){

	   @Override
	   public String convertResultToString(Object resultValue) {
	    return ((HashMap<String, String>)(resultValue)).get("Symbol");
	   }

	   @Override
	   protected FilterResults performFiltering(CharSequence constraint) {
	    if(constraint != null)
	    {
	     FilterResults filterResults = new FilterResults();
	     filterResults.values = mAllData;
	     filterResults.count = mAllData.size();
	     return filterResults;
	    }
	    else
	    {
	     return new FilterResults();
	    }
	   }

	   @Override
	   protected void publishResults(CharSequence constraint,
	     FilterResults results) {
	    if(results != null && results.count > 0)
	    {
	     notifyDataSetChanged();
	    }
	   }};

	   return nameFilter;
	 }		
	
}
